package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.DictionaryDao;
import entiey.Dictionary;
import util.JDBCUtil;
import util.ResultSetCreateObject;
/**
 * 根据字典接口的实现类
 * @author 秦洪涛
 *2020年8月21日下午9:10:52
 */
public class DictionaryDaoImpl implements DictionaryDao {

	@Override
	public int add(Dictionary t) {
		int num=0;
		num=JDBCUtil.executeUpdate("insert into dictionary(name,typeid) values(?,?)",
				t.getName(),t.getTypeid());
		return num;
	}

	@Override
	public int delete(int no) {
		int num=0;
		num=JDBCUtil.executeUpdate("delete from dictionary where id=?",no);
		return num;
	}

	@Override
	public int update(Dictionary t) {
		int num=0;
		num=JDBCUtil.executeUpdate("update  dictionary set name=?,typeid=?",
				t.getName(),t.getTypeid());
		return num;
	}

	@Override
	public List<Dictionary> findAll() {
		List<Dictionary> list=new ArrayList<Dictionary>();
		list=JDBCUtil.executeQuery("select * from dictionary", new ResultSetCreateObject<Dictionary>() {

			@Override
			public Dictionary create(ResultSet rs) {
				
				Dictionary d=new Dictionary();
				try {
					d.setId(rs.getInt("id"));
					d.setName(rs.getString("name"));
					d.setTypeid(rs.getInt("typeid"));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return d;
			}
		});
		return list;
	}

	@Override
	public Dictionary findById(int id) {
		List<Dictionary> list=new ArrayList<>();
		String sql="select * from dictionary where id=?";
		list=JDBCUtil.executeQuery(sql, new ResultSetCreateObject<Dictionary>() {

			@Override
			public Dictionary create(ResultSet rs) {
				Dictionary d=new Dictionary();
				try {
					d.setId(rs.getInt("id"));
					d.setName(rs.getString("name"));
					d.setTypeid(rs.getInt("typeid"));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return d;
			}
		},id);
		return list.get(0);
	}

	@Override
	public List<Dictionary> findDictionaryByTypeID(int tid) {
		return JDBCUtil.executeQuery("select * from dictionary where typeid=?", new ResultSetCreateObject<Dictionary>() {

			@Override
			public Dictionary create(ResultSet rs) {
				Dictionary d=new Dictionary();
				try {
					d.setId(rs.getInt("id"));
					d.setName(rs.getString("name"));
					d.setTypeid(rs.getInt("typeid"));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return d;
			}
		},tid);
//		return null;
	}

	@Override
	public Dictionary getDictionaryByTypeIDAndName(String name, int tid) {
		String sql="select * from dictionary where name=? and typeid=?";
		List<Dictionary> list=JDBCUtil.executeQuery(sql, new ResultSetCreateObject<Dictionary>() {

			@Override
			public Dictionary create(ResultSet rs) {
				Dictionary d=new Dictionary();
				try {
					d.setId(rs.getInt("id"));
					d.setName(rs.getString("name"));
					d.setTypeid(rs.getInt("typeid"));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return d;
			}
		},name,tid);
		return list.get(0);
	}

	public Dictionary instance(ResultSet rs) {
		Dictionary d=new Dictionary();
		try {
			d.setId(rs.getInt("id"));
			d.setName(rs.getString("name"));
			d.setTypeid(rs.getInt("typeid"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return d;
		
	} 
	
}
